const readline = require('readline');
const Tower = require('./Tower');

const getMilliseconds = function(duration) {
  const value = Math.round(duration) % 1000;
  return ' ' + value + ' milliseconds';
};

const getSeconds = function(duration) {
  const value = Math.round(duration / 1000) % 60;
  if (value) {
    return ' ' + value + ' seconds';
  }
  return '';
};

const getMinutes = function(duration) {
  const value = Math.round(duration / 60 / 1000) % 60;
  if (value) {
    return ' ' + value + ' minutes';
  }
  return '';
};

const getHours = function(duration) {
  const value = Math.round(duration / 60 / 60 / 1000) % 24;
  if (value) {
    return ' ' + value + ' hours';
  }
  return '';
};

const getDays = function(duration) {
  const value = Math.round(duration / 24 / 60 / 60 / 1000) % 30;
  if (value) {
    return ' ' + value + ' days';
  }
  return '';
};

const getMonths = function(duration) {
  const value = Math.round(duration / 30 / 24 / 60 / 60 / 1000) % 12;
  if (value) {
    return ' ' + value + ' months';
  }
  return '';
};

const getYears = function(duration) {
  const value = Math.round(duration / 12 / 30 / 24 / 60 / 60 / 1000);
  if (value) {
    return ' ' + value + ' years';
  }
  return '';
};

const toDurationString = function(duration) {
  let result = '';
  let count = 0;
  let functions = [getYears, getMonths, getDays, getHours, getMinutes, getSeconds, getMilliseconds];
  for (let i = 0; i < functions.length; i++) {
    if (count === 2) {
      return result;
    }
    if (functions[i](duration)) {
      result += functions[i](duration);
      count++;
    }
  }
  return result;
};

readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
}).on('line', function(line){
  const n = parseInt(line.split(' ')[0]);
  const control = line.split(' ')[1];
  if (control !== 'fake') {
    const t = new Tower(n);
    const startTime = new Date();
    t.solve(control === 'print');
    const duration = new Date() - startTime;
    console.log('finished in', toDurationString(duration));
  } else {
    const steps = Math.pow(2, n) - 1;
    console.log('it should take', steps, 'steps');
    const speed = 1000000;
    const duration = steps / speed;
    console.log('assuming 1 billion steps per seconds, it should finish in', toDurationString(duration));
  }
});
